<?php

class AdminUserModel
{
    private $adminUserKeyPrefix =   'A/';
    private $adminLogKeyPrefix  =   'AL/';

    public function getAdminUser($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'AM001';
            return  FALSE;
        }

        $key    =   $this->adminUserKeyPrefix. $info['uid'];
        $result =   Storage::getData($key);

        return  $result;
    }/*}}}*/

    public function getAdminList($info)/*{{{*/
    {
        $start  =   0;
        $length =   AC::ADMIN_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $orderBy=   ' id DESC ';
        $key    =   $this->adminUserKeyPrefix. 0;
        $result =   Storage::getManyData($key, $orderBy, $start, $length);

        return  $result;
    }/*}}}*/

    public function addAdminUser($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']) ||  empty($info['createUid'])   ||  empty($info['createAccount']))
        {
            AC::$errorNo    =   'AM002';
        }

        $data   =   $this->checkAdminOption($info);
        $data['uid']            =   $info['uid'];
        $data['account']        =   $info['account'];
        $data['createUid']      =   $info['createUid'];
        $data['createAccount']  =   $info['createAccount'];

        $key    =   $this->adminUserKeyPrefix. $info['uid'];
        $result =   Storage::addData($key, $data);
        return  $result;
    }/*}}}*/

    public function removeAdminUser($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'AM001';
            return  FALSE;
        }

        $key    =   $this->adminUserKeyPrefix. $info['uid'];
        $where  =   array('uid' =>  $info['uid']);
        $result =   Storage::delConditionData($key, $where);

        return  $result;
    }/*}}}*/

    public function setAdminUser($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'AM001';
            return  FALSE;
        }

        $data   =   $this->checkAdminOption($info);
        if (empty($data))
        {
            AC::$errorNo    =   'AM003';
            return  FALSE;
        }

        $key    =   $this->adminUserKeyPrefix. $info['uid'];
        $result =   Storage::setData($key, $data);

        return  $result;
    }/*}}}*/

    public function getAdminLogByUid($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            return  $this->getAdminLogById($info);
        }
        $start  =   0;
        $length =   AC::ADMIN_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $where  =   array('uid' =>  $info['uid']);
        $orderBy=   ' id DESC ';
        $key    =   $this->adminLogKeyPrefix. 0;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/

    public function getAdminLogById($info)/*{{{*/
    {
        $start  =   0;
        $length =   AC::ADMIN_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $orderBy=   ' id DESC ';
        $key    =   $this->adminLogKeyPrefix. 0;
        $result =   Storage::getManyData($key, $orderBy, $start, $length);

        return  $result;
    }/*}}}*/

    public function addAdminLog($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['account']) ||  empty($info['objectUid'])   ||  empty($info['objectAccount']))
        {
            AC::$errorNo    =   'AM004';
            return  FALSE;
        }
        if (empty($info['operation'])   ||  !isset($info['result']))
        {
            AC::$errorNo    =   'AM005';
            return  FALSE;
        }

        $data   =   array();
        $data['uid']            =   $info['uid'];
        $data['account']        =   $info['account'];
        $data['objectUid']      =   $info['objectUid'];
        $data['objectAccount']  =   $info['objectAccount'];
        $data['operation']      =   $info['operation'];
        $data['result']         =   $info['result'];
        $data['time']           =   AC::SERVER_TIME;

        $key    =   $this->adminLogKeyPrefix. 0;
        $result =   Storage::addData($key, $data);
        return  $result;
    }/*}}}*/

    private function checkAdminOption($options)/*{{{*/
    {
        #   apiOption   /*{{{*/
        $apiOption  =   array(
                            'registerUser'      =>  1,
                            'userLogin'         =>  1,
                            'getUserInfo'       =>  1,
                            'editUserInfo'      =>  1,
                            'updateLoginPasswd' =>  1,
                            'resetPasswd'       =>  1,
                            'webUserLogin'      =>  1,
                            'getUserList'       =>  1,
                            'getDiaryList'      =>  1,
                            'getDiaryInfo'      =>  1,
                            'writeDiary'        =>  1,
                            'editDiary'         =>  1,
                            'removeDiary'       =>  1,
                            'getFollowMeList'   =>  1,
                            'getMeFollowList'   =>  1,
                            'meFollow'          =>  1,
                            'removeFollow'      =>  1,
                            'getBlacklist'      =>  1,
                            'blacklistFollow'   =>  1,
                            'relieveBlackList'  =>  1,
                            'getFeedList'       =>  1,
                            'removeFeed'        =>  1,
                            'postComment'       =>  1,
                            'getCommentMeList'  =>  1,
                            'getMeCommentList'  =>  1,
                            'removeComment'     =>  1,
                            'postLike'          =>  1,
                            'getLikeMeList'     =>  1,
                            'getMeLikeList'     =>  1,
                            'removeLike'        =>  1,
                            'postShare'         =>  1,
                            'getShareMeList'    =>  1,
                            'getMeShareList'    =>  1,
                            'removeShare'       =>  1,
                            'cloudSync'         =>  1,
                            'cloudTraffic'      =>  1,
                            'cloudSyncList'     =>  1,
                            'cloudDownload'     =>  1,
                            'cloudUpdate'       =>  1,
                            'cloudRemove'       =>  1,
                            'getSquareList'     =>  1,
                            'postChat'          =>  1,
                            'getChatList'       =>  1,
                            'postBoard'         =>  1,
                            'updateBoard'       =>  1,
                            'removeBoard'       =>  1,
                            'getBoardList'      =>  1,
                            'postInform'        =>  1,
                            'updateInform'      =>  1,
                            'removeInform'      =>  1,
                            'getInformList'     =>  1,
                            'sendMessage'       =>  1,
                            'removeMessage'     =>  1,
                            'getMessageList'    =>  1,
                            'getMessageInfo'    =>  1,
                            'getMessageInbox'   =>  1,
                            'getMessageOutbox'  =>  1,
                        );/*}}}*/

        $result =   array();
        if (empty($options) ||  !is_array($options))    return  $result;

        foreach ($options   as  $option =>  $set)
        {
            if (empty($apiOption[$option])) continue;
            $result[$option]    =   $set;
        }
        return  $result;
    }/*}}}*/

}

